home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / so91.lha / Pattern / pattern.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-23  |  4.7 KB  |  155 lines

  1. ;/* Pattern.c.   AmigaMail pattern matching example.  Compiled with SAS/C 5.10a:
  2. lc -cfis -v -d0 -b0 -j73 Pattern.c
  3. blink from Pattern.o to Pattern lib lib:amiga.lib
  4. quit
  5. */
  6. /* (c)  Copyright 1991 Commodore-Amiga, Inc.   All rights reserved.
  7. The information contained herein is subject to change without notice,
  8. and is provided "as is" without warranty of any kind, either expressed
  9. or implied.  The entire risk as to the use of this information is
  10. assumed by the user.
  11. */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <dos/dos.h>
  16. #include <dos/dosasl.h>
  17. #include <dos/rdargs.h>
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/utility_protos.h>
  22.  
  23. /* define pragmas if you have them
  24. #define PRAGMAS */
  25. #ifdef PRAGMAS
  26. #include <pragmas/exec_pragmas.h>
  27. #include <pragmas/dos_pragmas.h>
  28. #include <pragmas/utility_pragmas.h>
  29. #else
  30. struct ExecBase *SysBase;
  31. struct DosLibrary *DOSBase;
  32. struct Library *UtilityBase;
  33. #endif
  34.  
  35. VOID            main(VOID);
  36. UWORD           StrLen(UBYTE *);
  37.  
  38. VOID main(VOID)
  39. {
  40. #ifdef PRAGMAS
  41.     struct DosLibrary *DOSBase;
  42.     struct Library *UtilityBase;
  43.  
  44. #endif
  45.  
  46.     struct RDArgs  *readargs;
  47.     LONG            rargs[3];
  48.     LONG            vargs[4];
  49.     UBYTE         **strings;
  50.     UBYTE          *pattern, *parsebuffer;
  51.     UWORD           case_sensitive, buffersize;
  52.     LONG            iswild, success;
  53.     COUNT           i;
  54.  
  55. #ifndef PRAGMAS
  56.     /* set up SysBase */
  57.     SysBase = (*((struct Library **) 4));
  58. #endif
  59.  
  60.     /* Fail silently if < 37 */
  61.     if (DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37))
  62.     {
  63.         UtilityBase = DOSBase->dl_UtilityBase;
  64.  
  65.         /* See the DOS Autodocs for more information about ReadArgs() */
  66.         if (readargs = ReadArgs("PATTERN/A,CASE/S,STRINGS/A/M", rargs, NULL))
  67.         {
  68.  
  69.             /* The pattern. */
  70.             pattern = (UBYTE *) (rargs[0]);
  71.  
  72.             /*
  73.              * Case sensitive or not? (default not. Note filename matching
  74.              * should ALWAYS be case insensitive).
  75.              */
  76.             case_sensitive = (UWORD) (rargs[1]);
  77.  
  78.             /* Pointer to array of strings to match */
  79.             strings = (UBYTE **) (rargs[2]);
  80.  
  81.             /* Get a buffer big enough to hold all the tokens */
  82.             buffersize = StrLen(pattern) * 3;
  83.  
  84.             if (parsebuffer = AllocMem(buffersize, MEMF_CLEAR))
  85.             {
  86.  
  87.                 /* Parse the pattern, according to case sensitivity flag */
  88.                 if (case_sensitive)
  89.                     iswild = ParsePattern(pattern, parsebuffer, buffersize);
  90.                 else
  91.                 {
  92.                     /* make pattern uppercase in case of character classes */
  93.                     i = 0;
  94.                     while (pattern[i])
  95.                         pattern[i] = ToUpper(pattern[i++]);
  96.                     iswild = ParsePatternNoCase(pattern, parsebuffer, buffersize);
  97.                 }
  98.  
  99.                 /*
  100.                  * -1 if ParsePattern() failed, 0 for no wildcards, 1 for
  101.                  * wildcards. For this I don't care if the supplied pattern had
  102.                  * wildcards or not.
  103.                  */
  104.                 if (iswild != -1)
  105.                 {
  106.                     /* The array of strings is terminated with a NULL */
  107.                     while (*strings)
  108.                     {
  109.  
  110.                         /*
  111.                          * MatchPattern() returns 1 for a successful match, 0
  112.                          * for no match
  113.                          */
  114.                         if (case_sensitive)
  115.                             success = MatchPattern(parsebuffer, *strings);
  116.                         else
  117.                             success = MatchPatternNoCase(parsebuffer, *strings);
  118.                         if (success)
  119.                         {
  120.                             vargs[0] = (LONG) * strings;
  121.                             VFPrintf(Output(), "Match: %s\n", vargs);
  122.                         }
  123.                         else
  124.                         {
  125.                             if (IoErr() != 0)
  126.                             {
  127.                                 VFPrintf(Output(), "Overflow\n", NULL);
  128.                                 break;
  129.                             }
  130.                         }
  131.                         strings++;
  132.                     }
  133.                 }
  134.                 else
  135.                     PrintFault(ERROR_BAD_TEMPLATE, pattern);
  136.                 FreeMem(parsebuffer, buffersize);
  137.             }
  138.             else
  139.                 PrintFault(ERROR_NO_FREE_STORE, NULL);
  140.             FreeArgs(readargs);
  141.         }
  142.         else
  143.             PrintFault(IoErr(), NULL);
  144.         CloseLibrary((struct Library *) DOSBase);
  145.     }
  146. }
  147.  
  148. UWORD StrLen(UBYTE * string)
  149. {
  150.     UBYTE          *length = string + 1;
  151.  
  152.     while (*string++ != '\0');
  153.     return ((UWORD) (string - length));
  154. }
  155.